import { ElMessage } from 'element-plus'
import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
    scrollBehavior (to, from, savedPosition) {
        return { top: 0, left: 0 }
    }
})

//路由拦截器
router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
        if (localStorage.getItem('token')) { //判断本地是否存在Token
            next();
        } else {
            next({
                path: '/login',
            })
        }
    } else {
        next();
    }
});
export default router
